package com.xsjl.user.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsjl.user.mapper.UserMapper;
import com.xsjl.user.pojo.User;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * user服务层
 * @author Administrator
 *
 */
@Service
public class UserService {

	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Autowired
	private BCryptPasswordEncoder passwordEncoder;

	/**
	 * 更新粉丝数
	 * @param userid
	 * @param count
	 */
	@Transactional
	public void updateFanCount(String userid, int count) {
		userMapper.updateFanCount(userid, count);
	}

	/**
	 * 更新关注数
	 * @param userid
	 * @param count
	 */
	@Transactional
	public void updateFollowCount(String userid, int count) {
		userMapper.updateFollowCount(userid, count);
	}
	public User findByUsernameAndPass(String username, String password) {

		QueryWrapper<User> wrapper = new QueryWrapper<>();
		wrapper.eq("username", username);
		User user = userMapper.selectOne(wrapper);
		if (user == null || !passwordEncoder.matches(password, user.getPassword())) {
			//return new Throwable("用户名或密码输入错误,请重新输入");
			return null;
		}
		return user;
	}

	/**
	 * 发生短信验证
	 * @param mobile		当前指邮箱号
	 */
	public void sendSms(String mobile) {
		String checkcode = RandomStringUtils.randomNumeric(6);
		System.out.println("random code:" + checkcode);
		//验证码放入redis
		redisTemplate.opsForValue().set("smscode_" + mobile, checkcode, 10, TimeUnit.MINUTES);
		//将验证码和手机号发送到rabbitmq
		Map<String, String> map = new HashMap<>();
		map.put("email", mobile);
		map.put("code", checkcode);
		// 使用exchange: xs_mq
		rabbitTemplate.convertAndSend("xs_mq","sms", map);
	}

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<User> findAll() {
		return userMapper.selectList(null);
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<User> findSearch(Map whereMap, int page, int size) {
		Page<User> pages = new Page(page, size);
		QueryWrapper<User> wrapper = new QueryWrapper<>();
		wrapper.allEq(whereMap);
		return userMapper.selectPage(pages, wrapper);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<User> findSearch(Map whereMap) {
		QueryWrapper<User> wrapper = new QueryWrapper<>();
		wrapper.allEq(whereMap);
		return userMapper.selectList(wrapper);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public User findById(String id) {
		return userMapper.selectById(id);
	}

	/**
	 * 增加
	 * @param user
	 */
	public void add(User user) {
		user.setPassword(passwordEncoder.encode(user.getPassword()));
		userMapper.insert(user);
	}

	/**
	 * 修改
	 * @param user
	 */
	public void update(User user) {
		userMapper.updateById(user);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		userMapper.deleteById(id);
	}





}
